<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\App;
use think\response\Json;

class Message extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Message';

    //关联模型
    protected array $hasOne = ['user'];

    //自定义方法排序
    protected array $sort = ['id' => 'desc'];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'uid', 'title' => '相关会员', 'templet' => '#_userTpl', 'width' => 240])
            ->addColumn(['field' => 'content', 'title' => '内容'])
            ->addColumn(['field' => 'create_time', 'title' => '反馈时间'])
            ->addColumn(['field' => 'status', 'title' => '状态'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 150])
            ->addRightButtons(['edit' => ['title' => '回复/查看', 'size' => '60%']])
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', 'input,Like', '', '\app\common\model\User', 'code|nickname|mobile,id'],
                ['content', '内容', 'input,Like']
            ])
            ->fetch();
    }

    public function edit($id = '', $tpl = 'message/edit', $info = ''): string
    {
        return parent::edit($id, $tpl, $info);
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $param['status'] = 2;
        $param['admin_id'] = $this->admin->id;
        $param['admin_name'] = $this->admin->admin_name;
        $param['reply_time'] = time();

        return parent::save($param, $model);
    }
}